home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS04.ADF
/
C
/
timrstuf.c
< prev
next >
Wrap
C/C++ Source or Header
|
1985-10-30
|
5KB
|
148 lines
/* timerstuff.c v1.0 */
/****************************************************************
* *
* Copyright 1985, Commodore Amiga Inc. All rights reserved. *
* No part of this program may be reproduced, transmitted, *
* transcribed, stored in retrieval system, or translated into *
* any language or computer language, in any form or by any *
* means, electronic, mechanical, magnetic, optical, chemical, *
* manual or otherwise, without the prior written permission of *
* Commodore Amiga Incorporated, 983 University Ave, #D *
* Los Gatos, CA 95030 *
* *
****************************************************************/
/* ******************************************************************** */
/* Exec Support Functions: CreateTimer, WaitTimer, DeleteTimer */
/*
/* CreateTimer() returns a pointer to an IOStdReq structure, set up
* for communications with the timer device. Returns NULL if it
* cannot allocate the timer or get enough memory or signals.
*
* WaitTimer(IOStdReq,seconds,microseconds) uses the communications
* block from CreateTimer(). It puts your task to sleep until the
* timer counts down at least this amount of time.
*
* SetTimer(IOStdReq,seconds,microseconds) returns a pointer to a port
* (copied from the IOStdReq) to which the timer returns the message
* block when the timeout has completed. It uses SendIO to transmit
* the IORequest. This means your task can go on to something else,
* then execute WaitPort(timerReplyPort), going to sleep until the
* timer has timed out. You will still have to do a GetMsg(timerReplyPort)
* after your task awakens, where with WaitTimer this is not necessary.
*
* DeleteTimer(IOStdReq) uses the block from CreateTimer() to free up
* memory and signal bits that have been allocated for this purpose.
*
* syntax Summary: struct IOStdReq *CreateTimer();
* int WaitTimer();
* struct Port *SetTimer();
* int DeleteTimer();
*
*
* typical program call:
*
* struct IOStdReq *timerA;
* struct Port *timerAPort;
* ...
* timerA = CreateTimer();
* if (timerA == NULL) exit(ERROR_IN_CREATETIMER);
* timerAPort = timerA->io_Message.mn_ReplyPort;
* ...
* WaitTimer(timerA,3,100000); wait at least 3.1 seconds
* ...
* SetTimer(timerA,2,0); tell timer to start
* while( DOING_SOMETHING )
* {
* do_it();
* if(CheckPort(timerAPort))
* { GetMsg(timerAPort); retrieve your msg
* do_something_else(); skipped unless timed out
* SetTimer(timerA,2,0); set timer again.
* }
*
* Author: Rob Peck 10/11/85
*/
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <exec/devices.h>
#include <devices/timer.h>
#define SECONDS io_Actual
#define MICROSECONDS io_Length
/* redefine fields in IOStdReq so as to match requirements of a timeval */
extern struct Port *CreatePort();
extern struct IOStdReq *CreateStdIO();
struct IOStdReq
*CreateTimer() /* return a pointer to an IOStdReq if
* it was possible to allocate a new
* timer */
{
SHORT error;
struct Port *timerport;
struct IOStdReq *timermsg;
timerport = CreatePort(0,0);
if (timerport == NULL)
return(NULL); /* Error during CreatePort */
timermsg = CreateStdIO(timerport);
if (timermsg == NULL)
{
DeletePort(timerport);
return(NULL); /* Error during CreateStdIO */
}
error = OpenDevice(TIMERNAME, UNIT_MICROHZ, timermsg, 0);
if (error != 0)
{
DeleteStdIO(timermsg);
DeletePort(timerport);
return(NULL); /* Error during OpenDevice */
}
return(timermsg);
}
struct Port
*SetTimer(whichtimer,seconds,microseconds)
ULONG seconds,microseconds;
struct IOStdReq *whichtimer;
{
struct Port *tempPort;
tempPort = whichtimer->io_Message.mn_ReplyPort;
whichtimer->io_Command = TR_ADDREQUEST; /* add a new timer request */
whichtimer->SECONDS = seconds; /* seconds */
whichtimer->MICROSECONDS = microseconds; /* microseconds */
SendIO(whichtimer);
return(tempPort);
}
int
WaitTimer(whichtimer,seconds,microseconds)
ULONG seconds,microseconds;
struct IOStdReq *whichtimer;
{
whichtimer->io_Command = TR_ADDREQUEST; /* add a new timer request */
whichtimer->SECONDS = seconds; /* seconds */
whichtimer->MICROSECONDS = microseconds; /* microseconds */
DoIO(whichtimer);
return(0);
}
int
DeleteTimer(whichtimer)
struct IOStdReq *whichtimer;
{
struct Port *whichport;
whichport = whichtimer->io_Message.mn_ReplyPort;
DeleteStdIO(whichtimer);
DeletePort(whichport);
return(0);
}